// pages/hotMusic/hotMusic.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    songsList:[],
    songIdList:[],
    albumImgList:[],
    albumImg:'',
    songName:'',
    idList:[],
    name:'',
    id:1867936253,
    count:6,
    shouimg:'../../image/收藏11.png',
    loopImg: '../../img/loop.png',
    playImg: '../../img/pause.png',
    isloop: 1
  },

 shouc(){
  var shouc_index=0
 shouc_index++
 if(shouc_index==0){
   this.setData({
   shouimg:'../../image/收藏11.png'
   })   
 }else if(shouc_index==1){
  this.setData({
    shouimg:'../../image/收藏11-ac.png'
    })  
    wx.showToast({
      title: '收藏成功',
      icon:'success'
    })
 }
 shouc_index--
  },

  input(event){
    var name = event.detail.value;
    this.setData({
      name: name
    })
  },
  playMusic() {
    if (this.data.isPlay) {
      this.music.pause()
      this.setData({
        isPlay: false,
        playImg: '../../img/play1.png',
        animation: 'paused'
      })
    } else {
      this.music.play()
      this.setData({
        isPlay: true,
        playImg: '../../img/pause.png',
        animation: 'play'
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var name = '张杰'
    if(name){
      wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 500
      })
      wx.request({
        url: 'https://music.163.com/api/search/get?s=' + name + '&type=1&limit=6',
        success: (res)=>{
          var list1 = []
          var list2 = []
          for(var i=0; i < res.data.result.songs.length; i++){
            var songId = res.data.result.songs[i].id
            list1.push(songId)
            wx.request({
              url: 'https://music.163.com/api/song/detail/?id=' + songId + '&ids=[' + songId +  ']',
              success: (res)=>{
                var imgUrl = res.data.songs[0].album.picUrl
                list2.push(imgUrl)
              }
            })
          }

          setTimeout(()=>{
            this.setData({
              songsList: res.data.result.songs,
              songIdList: list1
            })
          }, 1000)
        },
        fail: (res)=>{
          console.log(res)
        }
      })
      var list2 = options.idList
      var list3 = list2.split(',')
      wx.request({
        url: 'https://music.163.com/api/song/detail/?id=' + options.id + '&ids=[ '+ options.id + ']',
        success:(res)=>{
          var imgUrl = res.data.songs[0].album.picUrl
          var name = res.data.songs[0].name
          
          this.setData({
            albumImg: imgUrl,
            songName: name
          })
        }
      })
      this.setData({
        id: options.id,
        idList: list3
      })
    }else{
      wx.showToast({
        title: '请输入歌手名称',
        icon: "error"
      })
    }
  },
  click(){
    var name = this.data.name
    if(name){
      wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 500
      })

      wx.request({
        url: 'https://music.163.com/api/search/get?s=' + name + '&type=1&limit=6',
        success: (res)=>{
          var list1 = []
          var list2 = []
          for(var i=0; i < res.data.result.songs.length; i++){
            var songId = res.data.result.songs[i].id
            wx.request({
              url: 'https://music.163.com/api/song/detail/?id=' + songId + '&ids=[' + songId +  ']',
              success: (res)=>{
                var imgUrl = res.data.songs[0].album.picUrl
                list2.push(imgUrl)
              },
            })
            list1.push(songId)
          }

          setTimeout(()=>{
            this.setData({
              songsList: res.data.result.songs,
              songIdList: list1,
              albumImgList: list2
            })
          }, 1000)
        },
        fail: (res)=>{
          console.log(res)
        }
      })
    }else{
      wx.showToast({
        title: '请输入歌手名称',
        icon: "error"
      })
    }
  },
  lower(){
    var name = this.data.name
    var lower_count = this.data.count
    lower_count += 6
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 2000
    })
    wx.request({
      url: 'https://music.163.com/api/search/get?s=' + name + '&type=1&limit=' + lower_count,
      success: (res)=>{
        var songList = res.data.result.songs
        var list1 = [] // 存id
        var list2 = [] // 存图片路径
        for(var i = 0; i < songList.length; i++){
          list1.push(songList[i].id)
          var songId = songList[i].id
          wx.request({
            url: 'https://music.163.com/api/song/detail/?id=' + songId + '&ids=[' + songId +  ']',
            success: (res)=>{
              var imgUrl = res.data.songs[0].album.picUrl
              list2.push(imgUrl)
            }
          })
        }

        setTimeout(()=>{
          this.setData({
            songsList: songList,
            count: lower_count,
            songIdList: list1,
            albumImgList: list2
          })
        }, 2000)
      },
    })

    if(name==''){
      wx.showToast({
        title: '请输入歌手',
        icon: 'error',
        duration: 2000
      })
    }
  },
  nextMusic:function() {
    if (this.data.isloop == 0) {
      this.music.seek(0) 
      this.setData({
        isPlay: true,
        playImg: '../../img/pause.png',
        animation: 'play'
      })
      this.music.play()
    } else if(this.data.isloop == 1) {

      var idlist = this.data.idList 
      var id = this.data.id 
      var num = idlist.indexOf(id) 
      if (num >= idlist.length - 1) {
        var nextId = idlist[0]
      } else {
        var nextId = idlist[num + 1]
      }
      this.request(nextId)
    } else {
      var prevIdList = this.data.prevIdList
      var idlist = this.data.idList 
      var id = this.data.id 
      var num = idlist.indexOf(id) 
      prevIdList.push(idlist[num])
      var num1 = Math.floor(Math.random()*this.data.idList.length) 
      if(num == num1){
        if (num >= idlist.length - 1) {
          var nextId = idlist[0]
        } else {
          var nextId = idlist[num1 + 1]
        }
      }else{
        var nextId = idlist[num1]
      }
      this.request(nextId)
    }
  },

  prevMusic() {
    if (this.data.isloop == 0) {
      this.music.seek(0)
      this.setData({
        isPlay: true,
        playImg: '../../img/play.png',
        animation: 'play'
      })
      this.music.play()
    } else if(this.data.isloop == 1) {
      var idlist = this.data.idList
      var id = this.data.id
      var num = idlist.indexOf(id)
      if (num == 0) {
        var prevId = idlist[idlist.length - 1]
      } else {
        var prevId = idlist[num - 1]
      }
      this.request(prevId)
    } else{
      var idlist = this.data.idList
      var prevIdList = this.data.prevIdList
      var id = this.data.id
      var num = prevIdList.indexOf(id)

      if(num < 0){
        var prevId = prevIdList[prevIdList.length - 1]
      }else if(num == 0){
        var num1 = Math.floor(Math.random()*this.data.idList.length)
        if(num == num1){
          if (num >= idlist.length - 1) {
            var prevId = idlist[0]
          } else {
            var prevId = idlist[num1 + 1]
          }
        }else{
          var prevId = idlist[num1]
        }
      }else{
        var prevId = prevIdList[num - 1]
      }
      this.request(prevId)
    }
  },
  // 封装请求函数
  request: function(musicId){
    wx.request({
      url: 'https://music.163.com/api/song/detail/?id=' + musicId + '&ids=[ '+ musicId + ']',
      success: (res)=>{
        var imgUrl = res.data.songs[0].album.picUrl
        var name = res.data.songs[0].name
        this.setData({
          albumImg: imgUrl,
          songName: name,
          id: musicId, 
          isPlay: true,
          playImg: '../../img/pause.png',
          animation: 'play'
        })
        /* 播放音乐 */
        this.music.play()
      }
    })
  },
  toPlay(event){ 
    this.music.pause()
    var id = event.currentTarget.dataset.musicid  
    wx.navigateTo({
      url: '../play/play?id=' + id + '&idList=' + this.data.songIdList,
    })

  },

  onReady: function () {
    this.music = wx.createAudioContext('music')
    this.music.play()
  },
  
})